//
//  SDMyReporViewController.m
//  skinDetection
//
//  Created by  liangzhun on 2018/10/31.
//  Copyright © 2018年  liangzhun. All rights reserved.
//

#import "SDMyReporViewController.h"
#import "SDMyReportTableViewCell.h"
#import "SDCompareDetectionVC.h"
#import "SDEmptyView.h"

#import "SDMyReportData.h"
#import "SDMyReportModel.h"

#import "SDDetectionReportVC.h"

#import "SDAlertController.h"

#import "SDAnswersTestReportData.h"
#import "SDAnswersTestReportBaseClass.h"

#import "SDAnswerTestReportDetailVC.h"

static NSString *const cellIden = @"SDMyReportTableViewCell";
@interface SDMyReporViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) SDEmptyView *emptyView;//当没有报告时显示的空白页

@property (nonatomic ,strong) SDMyReportModel *showModel;

@property (nonatomic ,strong) NSMutableArray<SDAnswersTestReportData *> *myReportData;

@property (nonatomic ,strong) UIButton *selectedBtn;

@end

@implementation SDMyReporViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _navHeight.constant = SDNavBar_Height;
    self.selectedBtn = self.deviceReportBtn;
    [self.tableView setTableFooterView:[UIView new]];
    [self.tableView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellReuseIdentifier:cellIden];
    /*给空白页加约束*/
    [self.emptyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.left.right.equalTo(self.view);
        make.centerY.equalTo(self.view).offset(-55);
        make.height.mas_equalTo(115);
    }];
    
    [self loadData];
    
}

/**
 加载数据
 */
- (void)loadData
{
    [SDNetwork getGetAllQuestionReportWithFinish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
            if (!error) {
        
//                    self.showModel = [SDMyReportModel modelObjectWithDictionary:dic];
                SDAnswersTestReportBaseClass *class = [SDAnswersTestReportBaseClass modelObjectWithDictionary:dic];
                    self.myReportData = [NSMutableArray arrayWithArray:class.data];
                    self.emptyView.hidden = !(self.myReportData.count == 0);
                    [self.tableView reloadData];
                     self.emptyView.hidden = !(self.myReportData.count == 0);
                    // self.compareBtn.hidden = (self.myReportData.count == 0);
        
            }
    }];
//    [SDNetwork getMyCheckReportListWithFinish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
//
//
//        if (!error) {
//
//            self.showModel = [SDMyReportModel modelObjectWithDictionary:dic];
//            self.myReportData = [NSMutableArray arrayWithArray:self.showModel.data];
//            self.emptyView.hidden = !(self.myReportData.count == 0);
//            [self.tableView reloadData];
//             self.emptyView.hidden = !(self.myReportData.count == 0);
//            // self.compareBtn.hidden = (self.myReportData.count == 0);
//
//        }
//
//    }];
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    SDMyReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIden];
    cell.data = self.myReportData[indexPath.row];
    return cell;
    
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.myReportData.count;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsMake(0, 15, 0, 0)];
    }
    
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SDAnswersTestReportData *data = self.myReportData[indexPath.row];
    SDAnswerTestReportDetailVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDAnswerTestReportDetailVC class])];
    vc.data = data;
    [self.navigationController pushViewController:vc animated:YES];
    
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    UITableViewRowAction *action = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
       
        [self alertWithWithIndexPath:indexPath];
    }];
    [action setBackgroundColor:SDRedColor];
    return @[action];
}

/**
 删除报告
 
 @param indexPath 要删除的报告indexPath
 */
- (void)deleteReportWithIndexPath:(NSIndexPath *)indexPath
{
    SDAnswersTestReportData *data = self.myReportData[indexPath.row];
    [SDNetwork postDeleteQuestionReportWithParameter:[NSString stringWithFormat:@"%@",data.id] finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        
                if (!error) {
        
                    [self.myReportData removeObject:data];
                    [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                    [self.tableView reloadData];
                    self.emptyView.hidden = !(self.myReportData.count == 0);
                    self.compareBtn.hidden = (self.myReportData.count == 0);
        
                }
        
    }];
//    [SDNetwork postDeleteReportWithParameter:NSSTRINGAPPEND(@"/0/", data.id) finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
//
//        if (!error) {
//
//            [self.myReportData removeObject:data];
//            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//            [self.tableView reloadData];
//            self.emptyView.hidden = !(self.myReportData.count == 0);
//            self.compareBtn.hidden = (self.myReportData.count == 0);
//
//        }
//
//    }];
    
}

/**
 警告框
 
 @param indexPath 要删除的报告的indexPath
 */
- (void)alertWithWithIndexPath:(NSIndexPath *)indexPath
{
    SDAlertController *alert = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDAlertController class])];
    
    [alert.view setFrame:self.view.bounds];
    [self.view addSubview:alert.view];
    [self addChildViewController:alert];
    [alert setAlertContent:deleteReportAlertContent Title:deleteReportAlertTitle CancelTitle:deleteReportAlertCancel ConfirmTitle:deleteReportAlertConfirm];
    [alert.confirmBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^{
        
        [alert dismissView];
        [self deleteReportWithIndexPath:indexPath];
        
    }];
    
}
-(void)setSelectedBtn:(UIButton *)selectedBtn
{
    [self.selectedBtn setTitleColor:rgba(163, 161, 162, 1) forState:UIControlStateNormal];
    _selectedBtn = selectedBtn;
    [self.selectedBtn setTitleColor:rgba(255, 120, 155, 1) forState:UIControlStateNormal];
    
}
- (void)anmationLineView
{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.lineView.center = CGPointMake(self.selectedBtn.center.x, self.lineView.center.y);
    } completion:^(BOOL finished) {
        
    }];
    
}
-(SDEmptyView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [SDMethodTool getViewWithViewName:NSStringFromClass([SDEmptyView class])];
        _emptyView.hidden = YES;
        [self.view addSubview:_emptyView];
    }
    
    return _emptyView;
}
- (IBAction)selectDetectionReport:(UIButton *)sender {
    if (self.selectedBtn == sender)return;
    self.selectedBtn = sender;
    [self anmationLineView];
}

- (IBAction)back:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)compare:(UIButton *)sender {
    SDCompareDetectionVC *vc = (SDCompareDetectionVC *)[SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDCompareDetectionVC class])];
    vc.reportAllArray = self.showModel.data;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
